package classes.form {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	/**
	 * @author dennis
	 */
	public class Form extends Sprite{
		
		private const GET:String = "get";
		private const ARRAY:String = "array";
		private const ON_SUBMIT:String = "onSubmit";
		private const ON_CHANGE:String = "onChange";
		
		private var elements:Array = new Array();
		private var submitButton:Button;
		
		public function Form( action:String ):void{
			switch(action){
				case ON_CHANGE:
					addEventListener(Event.ENTER_FRAME, getData);
					break;
				case ON_SUBMIT:
					//Waiting until Submit-Button is registered
					break;
			}
		}
		
		public function registerElements(object:*):void{
			var exists:Boolean = checkElAlreadyRegistered(object);
			if( !exists ) this.elements.push( object );
		}
		
		private function checkElAlreadyRegistered(objectToCheck:*):Boolean{
			for(var i:int = 0; i<elements.length; i++){
				if(elements[i] == objectToCheck) return true;
			}
			return false;
		}
		
		public function submit(e:MouseEvent):void{
			trace(getData('get'));
		}
		
		public function reset(e:MouseEvent):void{
			for( var i:int = 0; i<elements.length; i++ ){
				elements[i].resetToDefault();
			}
		}
		
		public function getData(type:String):*{
			var data:*;
			switch( type ){
				case GET:
					for(var i:int = 0; i<elements.length; i++){
						var prev:String = i == 0 ? "" : "&";
						data = data+prev+elements[i].formName+"=";	
					}
					break;
				case ARRAY:
					data = elements;
					break;
			}	
			return data;
		}
	}
}
